﻿#include "loader.h"
#include <QLibrary>
#include "../TaiJi/interface.h"
#include <QApplication>
#include <QDebug>

typedef void* (*GetNewComponentFunc)();
typedef const char* (*GetComponentNameFunc)();
typedef const char* (*GetComponentDescriptionFunc)();


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    if(argc < 2){
        return 0;
    }
    QString strDllPath = argv[1];
    if(!strDllPath.contains(".dll")){
        return 0;
    }
    qDebug() << strDllPath;
    //插入运行的环境变量
    qputenv("PATH", QString("%1;%2").arg(qgetenv("PATH"), strDllPath.remove(".dll")).toLocal8Bit());
    QLibrary hdll(strDllPath.toLocal8Bit().data());
    if(hdll.load()){
        GetComponentNameFunc namefunc = (GetComponentNameFunc)hdll.resolve("GetComponentName");
        GetNewComponentFunc func = (GetNewComponentFunc)hdll.resolve("GetNewComponent");
        qDebug() << namefunc << func;
        if(namefunc&&func){
            TaiJiComponent* pCom = (TaiJiComponent*)func();
            if(argc >= 3){
                pCom->Excute(argv[2]);
            }else{
                pCom->Excute("");
            }
            return a.exec();
        }
    }else{
        qDebug() << hdll.errorString();
    }
    Loader w;
    w.show();
    return a.exec();
}
